package com.chenyi.file.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.chenyi.file.config.FileOssConfig;

import java.io.File;
import java.io.InputStream;

/**
 * 文件 阿里云 上传
 */
public class FileOssUtils {

    /**
     * 简单上传
     * @param key
     * @param file
     * @return
     */
    public static String uplaodSimple(String key,File file) throws Exception {
        OSS ossClient = new OSSClientBuilder().build(FileOssConfig.getEndpoint(), FileOssConfig.getAccessKeyId(), FileOssConfig.getAccessKeySecret());
        PutObjectRequest putObjectRequest = new PutObjectRequest(FileOssConfig.getBucketName(), key, file);
        ossClient.putObject(putObjectRequest);
        ossClient.shutdown();
        return "https://"+FileOssConfig.getBucketName()+"."+FileOssConfig.getEndpoint() + "/" + key;
    }
    /**
     * 简单上传
     * @param key
     * @param file
     * @return
     */
    public static String uplaodSimple(String key, InputStream inputStream) throws Exception {
        OSS ossClient = new OSSClientBuilder().build(FileOssConfig.getEndpoint(), FileOssConfig.getAccessKeyId(), FileOssConfig.getAccessKeySecret());
        PutObjectRequest putObjectRequest = new PutObjectRequest(FileOssConfig.getBucketName(), key, inputStream);
        ossClient.putObject(putObjectRequest);
        ossClient.shutdown();
        return "https://"+FileOssConfig.getBucketName()+"."+FileOssConfig.getEndpoint() + "/" + key;
    }
}
